package Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.avtivity.R;

import java.util.ArrayList;
import java.util.List;

import MyInterface.RecycleItemListener;
import bean.Searched;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

/**
 * Created by dell on 2017/3/21.
 */

public class SearchrecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{

    Context context;
    Searched Searched;
    public footerHolder mfooterHolder;
    String url = "";
    private RecycleItemListener RecycleItemListener;

    private List<bean.Searched.DataBean> list = new ArrayList<>();

    public SearchrecycleViewAdapter(Context context) {
        this.context = context;
    }

    public SearchrecycleViewAdapter(Context context, List<bean.Searched.DataBean> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        try {
            if (viewType == 1) {
                View view  = LayoutInflater.from(context).inflate(R.layout.searchresult_item, parent, false);
                myHolder holder = new myHolder(view);
                view.setOnClickListener(this);
                return holder;
            } else {
                mfooterHolder = new footerHolder(LayoutInflater.from(context).inflate(R.layout.search_footer, parent, false));
                return mfooterHolder;
            }
        } catch (Exception e) {
            Log.d(TAG, "onCreateViewHolder: ");
            return null;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof myHolder) {
                if (list.size() != 0) {
                    ((myHolder) holder).name.setText(list.get(position).getName());
                     ((myHolder) holder).itemView.setTag(list.get(position).getId());
                    ((myHolder) holder).price.setText("¥" + list.get(position).getPrice());
                    url = "http://www.chjcal.cc/" + list.get(position).getProtypeSet().get(0).getPic();
                    Glide.with(context).load(url).placeholder(R.mipmap.loading_64)
                            .error(R.mipmap.error_64).into(((myHolder) holder).pics);
                }
            }
            if (holder instanceof footerHolder) {

            }
        } catch (Exception e) {
            Log.d(TAG, "onBindViewHolder: ");
        }
    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == list.size()) {
            return 0;
        } else {
            return 1;
        }
    }


    @Override
    public void onClick(View v) {
        if (RecycleItemListener !=null){
            RecycleItemListener.onclick(v, (Integer) v.getTag());
        }
    }

    public class myHolder extends RecyclerView.ViewHolder {
        TextView name, price;
        ImageView pics;

        public myHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.name);
            price = (TextView) view.findViewById(R.id.price);
            pics = (ImageView) view.findViewById(R.id.pics);
        }
    }

    public class footerHolder extends RecyclerView.ViewHolder {
        View loadingview;
        View endview;
        View errorview;

        public footerHolder(View view) {
            super(view);
            loadingview = view.findViewById(R.id.loading_views);
            endview = view.findViewById(R.id.end_views);
            errorview = view.findViewById(R.id.error_views);
        }

        public void setdata(int status) {
            try {
                switch (status) {
                    case 0:
                        setAllGone();
                        break;
                    case 1:
                        setAllGone();
                        loadingview.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        setAllGone();
                        endview.setVisibility(View.VISIBLE);
                        break;
                    case 3:
                        setAllGone();
                        errorview.setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception E) {
                Log.d(TAG, "setdata: ");
            }
        }

        private void setAllGone() {
            if (loadingview != null) {
                loadingview.setVisibility(View.GONE);
            }
            if (endview != null) {
                endview.setVisibility(View.GONE);
            }
            if (errorview != null) {
                errorview.setVisibility(View.GONE);
            }
        }
    }

    /*
    list中加入更多数据
     */
    public void addall(List list, List listnew) {
        try{
            int lastIndex = list.size();
            if (list.addAll(listnew)) {
                notifyItemRangeInserted(lastIndex, list.size());
            }      
        }catch (Exception e){
            Log.d(TAG, "addall: ");
        }
      
    }

    public void setOnItemClickListener(RecycleItemListener listener) {
        this.RecycleItemListener = listener;
    }
}
